package net.roobook.cms.modules.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import net.roobook.cms.common.annotation.Log;
import net.roobook.cms.common.annotation.Log.OperatorType;
import net.roobook.cms.common.entity.SearchEntity;
import net.roobook.cms.common.entity.StateCodeEnum;
import net.roobook.cms.common.exception.CmsException;
import net.roobook.cms.common.web.BaseController;
import net.roobook.cms.modules.admin.entity.Menu;
import net.roobook.cms.modules.admin.entity.Permission;
import net.roobook.cms.modules.admin.service.MenuService;
import net.roobook.cms.modules.admin.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

/**
 * @author cos
 * @email 327878610@qq.com
 * @link https://roobook.net
 * @Description 权限controller
 * @date 2023/9/25 15:28
 **/
@Controller
@RequestMapping("/admin/permission")
public class PermissionController extends BaseController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 列表
     */
    @Log(operType = OperatorType.PAGE, module = "权限管理", content = "权限分页列表")
    @RequestMapping({"","/list"})
    public String list(Model model, SearchEntity params) {
        PageInfo<Permission> page = permissionService.queryListByPage(params);
        model.addAttribute("page", page);
        return "admin/permission/list";
    }

    /**
     * 添加跳转
     */
    @Log(operType = OperatorType.OTHER, module = "权限管理", content = "添加权限页面")
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<Menu> list = menuService.queryAll();
        model.addAttribute("menuList", list);
        return "admin/permission/add";
    }

    /**
     * 添加
     * @throws CmsException
     */
    @Log(operType = OperatorType.INSERT, module = "权限管理", content = "添加权限")
    @RequestMapping("/add")
    public String add(Model model, Permission permission) throws CmsException {
        permission.setId(IdUtil.getSnowflakeNextIdStr());
        permission.setCreateBy(getUserId());
        permission.setCreateTime(new Date());
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            throw new CmsException(
                    StateCodeEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                    StateCodeEnum.HTTP_INTERNAL_SERVER_ERROR.getDescription(),
                    e.getMessage());
        }
        return "redirect:/admin/permission/list";
    }


    /**
     * 编辑
     */
    @Log(operType = OperatorType.OTHER, module = "权限管理", content = "修改权限页面")
    @RequestMapping(value = "/toEdit", method = RequestMethod.GET)
    public String toEdit(Model model, String id) {
        List<Menu> list = menuService.queryAll();
        Permission permission = permissionService.queryMenuById(id);
        model.addAttribute("menuList", list);
        model.addAttribute("permission", permission);
        return "admin/permission/edit";
    }

    /**
     * 修改
     */
    @Log(operType = OperatorType.UPDATE, module = "权限管理", content = "修改权限")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Model model, Permission permission) {
        permission.setUpdateBy(getUserId());
        permission.setUpdateTime(new Date());
        permissionService.update(permission);
        return "redirect:/admin/permission/list";
    }

    /**
     * 删除
     */
    @Log(operType = OperatorType.DELETE, module = "权限管理", content = "删除权限")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Model model, String id) {
        permissionService.delete(id);
        return "redirect:/admin/permission/list";
    }
}
